package com.xuxin.baby.activity;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.lxj.xpopup.XPopup;
import com.xuxin.baby.R;
import com.xuxin.baby.base.BaseActivity;
import com.xuxin.baby.base.BasePagerAdapter;
import com.xuxin.baby.event.EatingBottleEvent;
import com.xuxin.baby.event.EatingBreastEvent;
import com.xuxin.baby.event.EatingDriedEvent;
import com.xuxin.baby.event.EatingHelpEvent;
import com.xuxin.baby.fragment.ChildEatingBottleFragment;
import com.xuxin.baby.fragment.ChildEatingBreastFragment;
import com.xuxin.baby.fragment.ChildEatingDriedFragment;
import com.xuxin.baby.fragment.ChildEatingHelpFragment;
import com.xuxin.baby.popup.EatPopup;
import com.xuxin.smartTabLayout.SlidingTabLayout;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by xu_xin
 * Created on 2020.08.17 0017
 * 记喂养
 */
public class ChildEatingRecordActivity extends BaseActivity {
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.button)
    Button button;
    @BindView(R.id.sliding_stl)
    SlidingTabLayout sliding_stl;
    @BindView(R.id.sliding_pager)
    ViewPager sliding_pager;
    private final ArrayList<Fragment> mFragments = new ArrayList<>();
    private final String[] mTitles = {"母乳", "瓶喂母乳", "奶粉", "辅食"};

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_sliding_eating);
    }

    @Override
    public void initView() {
        title_name.setText("记喂养");
        button.setText("保存");
        title_right.setImageResource(R.mipmap.img_question);
        title_right.setVisibility(View.VISIBLE);
        mFragments.add(ChildEatingBreastFragment.newInstance());
        mFragments.add(ChildEatingBottleFragment.newInstance());
        mFragments.add(ChildEatingDriedFragment.newInstance());
        mFragments.add(ChildEatingHelpFragment.newInstance());
        BasePagerAdapter myAdapter = new BasePagerAdapter(getSupportFragmentManager(), mFragments, mTitles);
        sliding_pager.setAdapter(myAdapter);
        sliding_stl.setViewPager(sliding_pager);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onComplete() {

    }

    @Override
    @OnClick({R.id.title_left, R.id.title_right, R.id.button})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.title_left) {
            finish();
        } else if (id == R.id.title_right) {
            showPopup();
        } else if (id == R.id.button) {
            setResult(200);
            int current = sliding_pager.getCurrentItem();
            if (current == 0) {
                EventBus.getDefault().post(new EatingBreastEvent());
            } else if (current == 1) {
                EventBus.getDefault().post(new EatingBottleEvent());
            } else if (current == 2) {
                EventBus.getDefault().post(new EatingDriedEvent());
            }else if (current == 3) {
                EventBus.getDefault().post(new EatingHelpEvent());
            }
        }
    }

    public void showPopup() {
        new XPopup.Builder(context)
                .dismissOnTouchOutside(true)
                .asCustom(new EatPopup(context))
                .show();
    }
}